package cn.wolfcode.web.controller;

import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.util.DateUtil;
import cn.wolfcode.util.UserUtil;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrderInfoController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private IOrderInfoService orderInfoService;

    public static ConcurrentHashMap<Long,Boolean> localhashMap = new ConcurrentHashMap<>();   //限制进入redis


    @RequestMapping("/doSeckill")
    @RequireLogin
    public Result<String > doSeckill(@RequestParam("seckillId") Long seckillId, @RequestParam("time") int time,HttpServletRequest request){

        if(StringUtils.isEmpty(seckillId+"")||StringUtils.isEmpty(time+"")){
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        Boolean flag = localhashMap.get(seckillId + "");
        if (flag!=null&&flag){
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }

        Object vo = redisTemplate.opsForHash().get(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time+""), seckillId + "");
        SeckillProductVo seckillProductVo = JSON.parseObject(vo.toString(), SeckillProductVo.class);
        Date startDate = seckillProductVo.getStartDate();
        boolean legalTime = DateUtil.isLegalTime(startDate, time);
//        if (!legalTime) {
//            throw new BusinessException(SeckillCodeMsg.TIME_OVER);
//        }





        if (seckillProductVo.getStockCount()<=0){
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }

        //判断redis
        String token = request.getHeader("token");
        UserInfo user = UserUtil.getUser(redisTemplate,token);
        Long phone = user.getPhone();
        Boolean member = redisTemplate.opsForSet().isMember(SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(time + ""), phone + ":" + seckillId);
        if (member){
            throw new BusinessException(SeckillCodeMsg.REPEAT_SECKILL);
        }

        Long increment = redisTemplate.opsForHash().increment(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time + ""), seckillId + "", -1);
        if (increment<0){
            localhashMap.put(seckillId,true);
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
//        String s=  seckillProductService.doSeckill(seckillId,time,phone);


        OrderMessage orderMessage=new OrderMessage();
        orderMessage.setSeckillId(seckillId);
        orderMessage.setTime(time);
        orderMessage.setUserPhone(phone);
        orderMessage.setToken(token);
        rocketMQTemplate.sendOneWay(MQConstant.ORDER_PEDDING_TOPIC,orderMessage);
        return Result.success("正在抢购中,请稍后");
    }





    @RequestMapping("/find")
    public Result<OrderInfo> find(@RequestParam("orderNo") String orderNo,HttpServletRequest request){
        if(StringUtils.isEmpty(orderNo)){
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }

        OrderInfo orderInfo = orderInfoService.find(orderNo);
        UserInfo user = UserUtil.getUser(redisTemplate, request.getHeader("token"));
        Long phone = user.getPhone();
        if (!phone.equals(orderInfo.getUserId())){
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success(orderInfo);

    }



}
